﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ImagedTextBox.ascx.cs"
    Inherits="Components.ImagedTextBox" %>
<%-- TODO: 考虑加一个div，以便css可以更准确地指定元素--%>
<script src='<% = ResolveUrl("~/Script/jquery-1.7.1.min.js") %>' type="text/javascript"></script>

<script type="text/javascript">
    function InsertString(tb, str)
    {
//        var tb = $tb.context;
//        console.log(tb);
        tb.focus();

        if (document.all)
        {
            var r = document.selection.createRange();
            document.selection.empty();
            r.text = str;
            r.collapse();
            r.select();
        }
        else
        {
            var newstart = tb.selectionStart + str.length;
            tb.value = tb.value.substr(0, tb.selectionStart) + str + tb.value.substring(tb.selectionEnd);
            tb.selectionStart = newstart;
            tb.selectionEnd = newstart;
        }
    }

    $(document).ready(function ()
    {
        $(".imagedTextBox .preview").click(function ()
        {
            var path = $(this).attr("src");
            var pid = path.substring(path.lastIndexOf('=') + 1, path.length);

            var container = $(this).parentsUntil(".imagedTextBox").parent();
            console.log(container);
            var tb = $(".descriptionTextBox", container)[0];
//            console.log("$tb");
//            console.log($tb);
//            console.log("$tb.get");
//            console.log($tb.get(0));
            
            InsertString(tb, "[" + pid + "]");

        });
    });

</script>
<div class="imagedTextBox">
    <div>
        <span id="itemLabel" runat="server">Description</span>
        <asp:TextBox ID="descriptionTextBox" runat="server" Rows="10" TextMode="MultiLine"
            CssClass="descriptionTextBox" />
        <a href="#" class="imagedTextBoxHelp">帮助</a>
    </div>
    <fieldset>
        <legend>Upload Photo</legend>
        <div id="imageContainer" runat="server">
            <div class="constraintLabel">
                <ol>
                    <li runat="server" id="fileSizeConstraintLabel" enableviewstate="False">Photo shall
                        not be larger than
                        <asp:Label runat="server" ID="fileSizeLabel" EnableViewState="False" />.</li>
                    <li runat="server" id="fileFormatConstraintLabel" enableviewstate="False">Acceptable
                        formats are
                        <asp:Label ID="acceptableFormatsLabel" runat="server" EnableViewState="False" />.</li>
                </ol>
            </div>
        </div>
        <div id="commandBar">
            <asp:FileUpload ID="FileUpload" runat="server" />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                ControlToValidate="FileUpload" ValidationGroup="ImagedTextBox_Image" Display="Dynamic" />
            <asp:CustomValidator ID="fileFormatValidator" runat="server" ErrorMessage="Incorrect Format."
                OnServerValidate="fileFormatValidator_ServerValidate" ControlToValidate="FileUpload"
                ValidationGroup="ImagedTextBox_Image" Display="Dynamic" />
            <asp:CustomValidator ID="fileSizeValidator" runat="server" ErrorMessage="File is too large."
                OnServerValidate="fileSizeValidator_ServerValidate" ControlToValidate="FileUpload"
                ValidationGroup="ImagedTextBox_Image" Display="Dynamic" />
            <asp:Button ID="uploadButton" runat="server" Text="Upload" ValidationGroup="ImagedTextBox_Image"
                OnClick="uploadButton_Click" />
        </div>
    </fieldset>
</div>
